package com.epower.web.action.app.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.epower.base.BaseAction;
import com.epower.bo.epower.commons.Result;
import com.epower.bo.epowerword.EpowerWordService;
import com.epower.dataobject.EpowerWordDO;

/**
 * @author bolin.hbc
 * 
 *         单词的action
 * 
 */
@Scope("prototype")
@Component("wword")
public class WordAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private Integer bookid;
	private Integer id;

	private EpowerWordService epowerWordBO;

	private Result result = Result.succResult();

	public String doGetWordById() {
		if (id == null) {
			result = Result.errorResult(Result.RESULTCODE_REQUEST_FAILURE, "id can't be null");
			return ERROR;
		}

		EpowerWordDO word = epowerWordBO.getEpowerWordById(id);
		if (word == null) {
			result = Result.errorResult(Result.RESULTCODE_REQUEST_FAILURE, "word is not exist");
			return ERROR;
		}
		TmpWord tmp = new TmpWord();
		tmp.setWordid(word.getId());
		tmp.setBookid(word.getBookid());
		tmp.setSpelling(word.getSpelling());
		tmp.setPhonetics(word.getPhonetics());
		tmp.setMeaning(word.getMeaning());
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("word", tmp);
		result.setResult(resultMap);
		return SUCCESS;
	}

	public String doGetWordsByBookId() {

		if (bookid == null) {
			result = Result.errorResult(Result.RESULTCODE_REQUEST_FAILURE, "bookid can't be null");
			return ERROR;
		}
		EpowerWordDO word = new EpowerWordDO();
		word.setBookid(bookid);
		List<EpowerWordDO> words = epowerWordBO.getEpowerWordListByOption(word);
		List<TmpWord> tmps = new ArrayList<TmpWord>();

		for (EpowerWordDO w : words) {
			TmpWord tmp = new TmpWord();
			tmp.setWordid(w.getId());
			tmp.setSpelling(w.getSpelling());
			tmp.setPhonetics(w.getPhonetics());
			tmp.setMeaning(w.getMeaning());
			tmps.add(tmp);
		}

		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("words", tmps);
		result.setResult(resultMap);
		return SUCCESS;
	}

	public class TmpWord {
		private Integer wordid;
		private Integer bookid;
		private String spelling;
		private String phonetics;
		private String meaning;

		public Integer getWordid() {
			return wordid;
		}

		public void setWordid(Integer wordid) {
			this.wordid = wordid;
		}

		public Integer getBookid() {
			return bookid;
		}

		public void setBookid(Integer bookid) {
			this.bookid = bookid;
		}

		public String getSpelling() {
			return spelling;
		}

		public void setSpelling(String spelling) {
			this.spelling = spelling;
		}

		public String getPhonetics() {
			return phonetics;
		}

		public void setPhonetics(String phonetics) {
			this.phonetics = phonetics;
		}

		public String getMeaning() {
			return meaning;
		}

		public void setMeaning(String meaning) {
			this.meaning = meaning;
		}

	}

	public Integer getBookid() {
		return bookid;
	}

	public void setBookid(Integer bookid) {
		this.bookid = bookid;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public Result getResult() {
		return result;
	}

	public void setResult(Result result) {
		this.result = result;
	}

	public void setEpowerWordBO(EpowerWordService epowerWordBO) {
		this.epowerWordBO = epowerWordBO;
	}

}
